package com.sca;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/dubbo-demo")
public class DemoController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @DubboReference
    private DemoDubboService demoDubboService;

    /**
     * Dubbo服务 演示功能,打招呼,内部调用Dubbo服务
     * <p>
     * 不经过网关的访问方法 : http://localhost:8083/dubbo-demo/hello?name=tom
     *
     * @param name
     * @return
     */
    @GetMapping("/hello")
    public String hello(String name) {
        // 2025-02-01 注意 : SCA微服务和Dubbo微服务的链路跟踪是不通的两个域,现在是在 SCA微服务范围内，这里无论代码方式输出
        // traceId+spanId, 还是日志方式输出 traceId+spanId,  这里都是显示 SCA微服务范围的相应信息.

        // 获取当前的 Span
        Span currentSpan = Span.current();
        SpanContext spanContext = currentSpan.getSpanContext();
        logger.info("trace context: {},{}", spanContext.getTraceId(), spanContext.getSpanId());

        String message = demoDubboService.sayHello(name);
        logger.info("greeting message: {}", message);
        return message;
    }
}
